#include <stdio.h>
#include <stdlib.h>
#include "game.h"


int array_2048[ROW][LINE] = {0};

static int judge_rand()
{
	int i,j;
	for(i = 0;i < ROW;i++)
	{
		for(j = 0;j < LINE;j++)
		{
			if(array_2048[i][j] == 0)
					return 0;
		}
	}
	return 1;
}

void function()
{
		cr_random();
		cr_random();
		interface();

		while(1)
		{
				char ch;
				int ret,flag;
				scanf("%c%*c",&ch);
				if(ch == '#')
					break;
				ret = judge_move(ch);
			
				if(ret == 1)
				{
						switch(ch)
						{
								case 'w':
										move_up();
										break;
								case 's':
										move_down();
										break;
								case 'a':
										move_left();
										break;
								case 'd':
										move_right();
										break;
						}
						flag = judge_rand();
						if(flag == 1)
						{
								printf("fild!\n");
								exit(0);	
						}
						else
							cr_random();
						interface();
						ret = judge_move(ch);
						if(ret == 2)
						{
								printf("succeed!\n");
								break;
						}
						else if(ret == 3)
						{
								printf("fild!\n");
								break;
						}

				}
				else if(ret == 2)
				{
						printf("succeed!\n");
						exit(0);
				}
				else if(ret == 3)
				{
						printf("fild!\n");
						exit(0);
				}
				else if(ret == 4)
				{
						printf("no move!\n");
				}
		}
}

void interface()
{
		int i,j;
		printf("\n\033[2J\033[10;1H\033[42m[][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][]\033[0m\n");
		for(i = 0;i < ROW;i++)
		{
				printf("\033[42m[]\t\033[0m");
				for(j = 0;j < LINE;j++)
				{
						printf("%d\t\033[42m[]\033[0m\t",array_2048[i][j]);
				}
				printf("\n");
		}
		printf("\033[?25l\033[42m[][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][]\033[0m\n\n");
}


void cr_random()
{
		int randvaule = rand()%2;

		if(randvaule == 0)
		{
				randvaule = 2;
		}
		else
		{
				randvaule = 4;
		}

		while(1)
		{
				int row,line;
				line = rand()%4;
				row = rand()%4;
				if(array_2048[row][line] == 0)
				{
						array_2048[row][line] = randvaule;
						break;
				}
				continue;
		}
}

int judge_left()
{
		int i,j;

		for(i = 0;i < ROW;i++)
		{
				for(j = 0;j < LINE-1;j++)
				{
						if(array_2048[i][j] == array_2048[i][j+1] && array_2048[i][j] > 0)
								return 1;
						if(array_2048[i][j] != array_2048[i][j+1] && array_2048[i][j] == 0)
								return 1;
				}
		}
		return 0;
}


int judge_right()
{
		int i,j;

		for(i = 0;i < ROW;i++)
		{
				for(j = LINE-1;j > 0;j--)
				{
						if(array_2048[i][j] == array_2048[i][j-1] && array_2048[i][j] > 0)
								return 1;
						if(array_2048[i][j] != array_2048[i][j-1] && array_2048[i][j] == 0)
								return 1;
				}
		}
		return 0;
}


int judge_up()
{
		int i,j;

		for(i = 0;i < ROW-1;i++)
		{
				for(j = 0;j < LINE;j--)
				{
						if(array_2048[i][j] == array_2048[i+1][j] && array_2048[i][j] > 0)
								return 1;
						if(array_2048[i][j] != array_2048[i+1][j] && array_2048[i][j] == 0)
								return 1;
				}
		}
		return 0;
}


int judge_down()
{
		int i,j;

		for(i = ROW-1;i > 0;i--)
		{
				for(j = 0;j < LINE;j--)
				{
						if(array_2048[i][j] == array_2048[i-1][j] && array_2048[i][j] > 0)
								return 1;
						if(array_2048[i][j] != array_2048[i-1][j] && array_2048[i][j] == 0)
								return 1;
				}
		}
		return 0;
}

int judge_move(char ch)
{
		int i,j;
		for(i = 0;i < ROW;i++)
		{
				for(j = 0;j < LINE;j++)
				{
						if(array_2048[i][j] == 2048)
								return 2;//成功
				}
		}

		int ret = 0;
		switch('w')
		{
				case 'w':ret += judge_up();
				case 'a':ret += judge_left();
				case 's':ret += judge_down();
				case 'd':ret += judge_right();
		}
		if(ret == 0)
				return 3;//失败
		ret = 0;
		switch(ch)
		{
				case 'w':ret += judge_up();
						 break;
				case 'a':ret += judge_left();
						 break;
				case 's':ret += judge_down();
						 break;
				case 'd':ret += judge_right();
						 break;
				default:
						 printf("Input error!\n");
						 break;
		}
		if(ret)
				return 1;//执行移动
		else
				return 4;//不能移动
}

void move_up()
{
		int i,j,k;
		for(i = 0;i < ROW-1;i++)
		{
				for(j = 0;j < LINE;j++)
				{
						if(array_2048[i][j] != 0)
						{
								for(k = i+1;k < ROW;k++)
								{
										if(array_2048[i][j] != array_2048[k][j] && array_2048[k][j] != 0)
												break;
										else if(array_2048[i][j] == array_2048[k][j])
										{
												array_2048[i][j] *= 2;
												array_2048[k][j] = 0;
												break;
										}
								}
						}
				}
		}

		for(i = 0;i < ROW-1;i++)
		{
				for(j = 0;j < LINE;j++)
				{
						if(array_2048[i][j] == 0)
						{
								for(k = i+1;k < ROW;k++)
								{
										if(array_2048[k][j] != 0)
										{
												array_2048[i][j] = array_2048[k][j];
												array_2048[k][j] = 0;
												break;
										}
								}
						}
				}
		}
}

void move_down()
{
		int i,j,k;
		for(i = ROW-1;i > 0;i--)
		{
				for(j = 0;j < LINE;j++)
				{
						if(array_2048[i][j] != 0)
						{
								for(k = i-1;k > -1;k--)
								{
										if(array_2048[i][j] != array_2048[k][j] && array_2048[k][j] != 0)
												break;
										else if(array_2048[i][j] == array_2048[k][j])
										{
												array_2048[i][j] *= 2;
												array_2048[k][j] = 0;
												break;
										}
								}
						}
				}
		}

		for(i = ROW-1;i > 0;i--)
		{
				for(j = 0;j < LINE;j++)
				{
						if(array_2048[i][j] == 0)
						{
								for(k = i-1;k > -1;k--)
								{
										if(array_2048[k][j] != 0)
										{
												array_2048[i][j] = array_2048[k][j];
												array_2048[k][j] = 0;
												break;
										}
								}
						}
				}
		}
}

void move_left()
{
		int i,j,k;
		for(i = 0;i < ROW;i++)
		{
				for(j = 0;j < LINE-1;j++)
				{
						if(array_2048[i][j] != 0)
						{
								for(k = j+1;k < LINE;k++)
								{
										if(array_2048[i][j] != array_2048[i][k] && array_2048[i][k] != 0)
												break;
										else if(array_2048[i][j] == array_2048[i][k])
										{
												array_2048[i][j] *= 2;
												array_2048[i][k] = 0;
												break;
										}
								}
						}
				}
		}

		for(i = 0;i < ROW;i++)
		{
				for(j = 0;j < LINE-1;j++)
				{
						if(array_2048[i][j] == 0)
						{
								for(k = j+1;k < LINE;k++)
								{
										if(array_2048[i][k] != 0)
										{
												array_2048[i][j] = array_2048[i][k];
												array_2048[i][k] = 0;
												break;
										}
								}
						}
				}
		}
}

void move_right()
{
		int i,j,k;
		for(i = 0;i < ROW;i++)
		{
				for(j = LINE-1;j > 0;j--)
				{
						if(array_2048[i][j] != 0)
						{
								for(k = j-1;k > -1;k--)
								{
										if(array_2048[i][j] != array_2048[i][k] && array_2048[i][k] != 0)
												break;
										else if(array_2048[i][j] == array_2048[i][k])
										{
												array_2048[i][j] *= 2;
												array_2048[i][k] = 0;
												break;
										}
								}
						}
				}
		}

		for(i = 0;i < ROW;i++)
		{
				for(j = LINE-1;j > 0;j--)
				{
						if(array_2048[i][j] == 0)
						{
								for(k = j-1;k > -1;k--)
								{
										if(array_2048[i][k] != 0)
										{
												array_2048[i][j] = array_2048[i][k];
												array_2048[i][k] = 0;
												break;
										}
								}
						}
				}
		}
}



